package com.chandra.pos.order.web;

import com.chandra.pos.customer.dao.entity.Customer;
import com.chandra.pos.order.dao.entity.Order;
import com.chandra.pos.order.dao.entity.OrderItem;
import com.chandra.pos.order.dao.entity.OrderItemDataModel;
import com.chandra.pos.order.service.IOrderService;
import com.chandra.pos.product.dao.entity.Product;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * Created with IntelliJ IDEA.
 * Customer: T4671
 * Date: 10/5/13
 * Time: 10:23 AM
 * To change this template use File | Settings | File Templates.
 */
@Named("orderMB")
@SessionScoped
public class OrderBean  implements Serializable {


    private static final Logger logger = LoggerFactory.getLogger(OrderBean.class);

    @Inject
    IOrderService orderService;

    private static final String SUCCESS = "success";
    private static final String ERROR = "error";

    private Long id;

    private Boolean isCompleteAllowed=false;

    private Boolean hideSaleDialog=false;

    private OrderItemDataModel orderItems;

    private  OrderItem selectedItem;

    public Boolean getCompleteAllowed() {
        return isCompleteAllowed;
    }

    public void setCompleteAllowed(Boolean completeAllowed) {
        isCompleteAllowed = completeAllowed;
    }

    public OrderItem getSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(OrderItem selectedItem) {
        this.selectedItem = selectedItem;
    }

    private Order order =new Order();

    public OrderItemDataModel getOrderItems() {
        return orderItems;
    }

    public void setOrderItems(OrderItemDataModel orderItems) {
        this.orderItems = orderItems;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    private BigDecimal salePrice;

    private BigDecimal  costPrice;

    private BigDecimal quantity;

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }

    public BigDecimal getSalePrice() {
        return salePrice;
    }

    public void setSalePrice(BigDecimal salePrice) {
        this.salePrice = salePrice;
    }

    public BigDecimal getCostPrice() {
        return costPrice;
    }

    public void setCostPrice(BigDecimal costPrice) {
        this.costPrice = costPrice;
    }



    public void completeSale(){

        logger.info("completeSale");
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Boolean getHideSaleDialog() {
        return hideSaleDialog;
    }

    public void setHideSaleDialog(Boolean hideSaleDialog) {
        this.hideSaleDialog = hideSaleDialog;
    }

    private Product selectedProduct;

    private Customer selectedCustomer;


    public OrderBean() {

        orderItems = new OrderItemDataModel(getOrder().getItems());
    }


    public void handleSelectProduct(SelectEvent event) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected:" + event.getObject().toString(), null);
        this.selectedProduct = (Product) event.getObject();
        this.salePrice=this.selectedProduct.getSalePrice();
        hideSaleDialog=true;

        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void handleSelectCustomer(SelectEvent event) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected:" + event.getObject().toString(), null);
        this.selectedCustomer = (Customer) event.getObject();
        this.order.setCustomer(this.selectedCustomer);
        this.selectedCustomer=null;
        FacesContext.getCurrentInstance().addMessage(null, message);
    }



    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }

    public void addSelectedItem() {

        this.hideSaleDialog=false;


         order.addOrderItem(selectedProduct,quantity, salePrice,costPrice);
        this.selectedProduct=null;
    }

    public void cancelSelectedItem() {

        this.hideSaleDialog=false;

    }
    public void removeSelectedItem() {

        this.hideSaleDialog=false;

    }

    public Product getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(Product selectedProduct) {
        this.selectedProduct = selectedProduct;
    }

    public void onRowSelect(SelectEvent event) {
        FacesMessage msg = new FacesMessage("Item Selected", ((OrderItem) event.getObject()).getProduct().getName());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void onRowUnselect(UnselectEvent event) {
        FacesMessage msg = new FacesMessage("Item Unselected", ((OrderItem) event.getObject()).getProduct().getName());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }


}
